package controller

import (
	"context"
	v1 "itam/api/v1"
	"itam/internal/model"
	"itam/internal/service"
)

type cPm struct{}

var Pm = cPm{}

func (c *cPm) Create(ctx context.Context, req *v1.PmCreateReq) (res *v1.PmCreateRes, err error) {
	out, err := service.Pm().Create(ctx, model.PmCreateInput{
		Name:           req.Name,
		OsType:         req.OsType,
		Owner:          req.Owner,
		Cpu:            req.Cpu,
		Memory:         req.Memory,
		Disk:           req.Disk,
		Brand:          req.Brand,
		Model:          req.Model,
		IpAddress:      req.IpAddress,
		MachineUsage:   req.MachineUsage,
		PurchaseDate:   req.PurchaseDate,
		RecomandStatus: req.RecomandStatus,
	})
	if err != nil {
		return nil, err
	}
	return &v1.PmCreateRes{Id: out.Id}, nil
}

func (c *cPm) Delete(ctx context.Context, req *v1.PmDeleteReq) (res *v1.PmDeleteRes, err error) {
	err = service.Pm().Delete(ctx, req.Id)
	return
}

func (c *cPm) Update(ctx context.Context, req *v1.PmUpdateReq) (res *v1.PmUpdateRes, err error) {
	err = service.Pm().Update(ctx, model.PmUpdateInput{
		Id:             req.Id,
		OsType:         req.OsType,
		Owner:          req.Owner,
		Cpu:            req.Cpu,
		Memory:         req.Memory,
		Disk:           req.Disk,
		Brand:          req.Brand,
		Model:          req.Model,
		IpAddress:      req.IpAddress,
		MachineUsage:   req.MachineUsage,
		PurchaseDate:   req.PurchaseDate,
		RecomandStatus: req.RecomandStatus,
	})
	if err != nil {
		return nil, err
	}
	return
}

func (c *cPm) GetList(ctx context.Context, req *v1.PmGetListReq) (res *v1.PmGetListRes, err error) {
	out, err := service.Pm().GetList(ctx, model.PmGetListInput{
		OsType:         req.OsType,
		Owner:          req.Owner,
		Brand:          req.Brand,
		Model:          req.Model,
		IpAddress:      req.IpAddress,
		RecomandStatus: req.RecomandStatus,
		Page:           req.Page,
		Size:           req.Size,
	})
	if err != nil {
		return nil, err
	}
	return &v1.PmGetListRes{
		List:  out.List,
		Page:  out.Page,
		Size:  out.Size,
		Total: out.Total,
	}, nil
}

func (c *cPm) Detail(ctx context.Context, req *v1.PmDetailReq) (res *v1.PmDetailRes, err error) {
	out, err := service.Pm().Detail(ctx, req.Id)
	if err != nil {
		return nil, err
	}
	return &v1.PmDetailRes{
		VmList: out.VmList,
	}, nil
}
